package com.ks.community.service.impl;

import com.ks.community.entity.Ingredient;
import com.ks.community.entity.Menu;
import com.ks.community.entity.MenuIngredient;
import com.ks.community.entity.vo.MenuVo;
import com.ks.community.mapper.MenuMapper;
import com.ks.community.service.IngredientService;
import com.ks.community.service.MenuIngredientService;
import com.ks.community.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dCoder
 * @since 2022-03-29
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    MenuIngredientService menuIngredientService;
    @Autowired
    IngredientService ingredientService ;
    @Override
    public String saveOne(MenuVo menuVo) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuVo,menu);
        menu.setLikedNumber(0);
        menu.setCollectedNumber(0);
        baseMapper.insert(menu);
        int n = menuVo.getIngredients().size();
        System.out.println(n);
        List<Ingredient> ingredients= menuVo.getIngredients();
        for (int i = 0; i < n; i++) {
            Ingredient ingredient = new Ingredient();
            BeanUtils.copyProperties(ingredients.get(i),ingredient);
            String ingredientId = ingredientService.selectByName(ingredient.getIngredientName());
            MenuIngredient menuIngredient = new MenuIngredient();
            menuIngredient.setIngredientId(ingredientId);
            menuIngredient.setMenuId(menu.getMenuId());
            menuIngredientService.addOne(menuIngredient);
        }
        return menu.getMenuId();

    }


}
